package piccolo;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.ComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import xml.XMLWriter;
import dbConnections.MySQLConnection;
import dbStruct.DatabaseStructure;

public class mySqlPane extends JPanel implements MouseListener,ActionListener{
	private static final long serialVersionUID = 6147525712149375493L;
	private HashMap<String, JComponent> textFields = new HashMap<String, JComponent>();
	
	public mySqlPane()
	{
		super(true);
		this.setLayout(new GridLayout(6, 2, 5, 5));
		createFields();
		createButtons();
		this.setVisible(true);
	}
	
	private void createFields()
	{
		JLabel label;
		JTextField text;
		JPasswordField pwd;
		JComboBox combo;
		
		label = new JLabel("HOST");
		this.add(label);
		text = new JTextField();
		text.setName("txtPath");
		this.add(text);
		textFields.put("txtPath", text);
		
		label = new JLabel("USER");
		this.add(label);
		text = new JTextField();
		text.setName("txtUser");
		this.add(text);
		textFields.put("txtUser", text);

		label = new JLabel("PASSWORD");
		this.add(label);
		text = new JPasswordField();
		text.setName("txtPwd");
		this.add(text);
		textFields.put("txtPwd", text);
		
		label = new JLabel();
		this.add(label);
		JButton button = new JButton("DATABASE");
		button.addActionListener(this);
		this.add(button);
		
		label = new JLabel("DATABASE");
		this.add(label);
		combo=new JComboBox();
		combo.setEditable(false);
		combo.setName("cmbDB");
		combo.addActionListener(this);
		this.add(combo);
		textFields.put("cmbDB", combo);	
	}
	
	private void createButtons()
	{
		JButton button;
		button = new JButton("ANNULLA");
		button.addActionListener(this);
		this.add(button);
		
		button = new JButton("CONVERTI");
		button.addActionListener(this);
		this.add(button);
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if(button.getText().equalsIgnoreCase("ANNULLA"))
			{
				mySQLSelector.getInstance().setVisible(false);
			}
			if(button.getText().equalsIgnoreCase("DATABASE"))
			{
				String path = ((JTextField) textFields.get("txtPath")).getText();
				String user = ((JTextField) textFields.get("txtUser")).getText();
				String pwd  = ((JTextField) textFields.get("txtPwd")).getText();
				String[] dbs =  MySQLConnection.getInstance().getDatabases(path, user, pwd);
				JComboBox cmb = (JComboBox) textFields.get("cmbDB");
				cmb.removeAllItems();
				for(int i=0;i<dbs.length;i++)
				{
					cmb.addItem(dbs[i]);
				}
			}
			if(button.getText().equalsIgnoreCase("CONVERTI"))
			{
				String path = ((JTextField) textFields.get("txtPath")).getText();
				String user = ((JTextField) textFields.get("txtUser")).getText();
				String pwd  = ((JTextField) textFields.get("txtPwd")).getText();
				String db   = ((JComboBox) textFields.get("cmbDB")).getSelectedItem().toString();
				
				MySQLConnection.getInstance().getConnection(db,user,pwd);
				DatabaseStructure dbStruct = MySQLConnection.getInstance().getStructure();
				
				XMLWriter xml = new XMLWriter(dbStruct);
				try {
					File f = new File("d:\\testXML.xml");
					xml.create();
					xml.write(f);
					
					ReadXML readXML = new ReadXML(f);
					
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				
				mySQLSelector.getInstance().setVisible(false);
				
			}
		}
	}
}
